<script setup>
import { onMounted, ref } from "vue"
import { Search, Edit, Delete, DocumentAdd } from '@element-plus/icons-vue'
import axios from '@/util/axios'
import { getServerUrl } from '@/util/axios'
import Dialog from './components/dialog.vue'
import ImageDialog from './components/imageDialog.vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const queryForm = ref({
  query: '',
  pageNum: 1,
  pageSize: 10
})

const total = ref(0)
const tableData = ref([])
const initBigTypeList = async () => {
  const res = await axios.post('admin/bigType/list', queryForm.value)
  tableData.value = res.data.bigTypeList
  total.value = res.data.total
}
onMounted(() => {
  initBigTypeList()
})

const handleSizeChange = (pageSize) => {
  queryForm.value.pageNum = 1
  queryForm.value.pageSize = pageSize
  initBigTypeList()
}
const handleCurrentChange = (pageNum) => {
  queryForm.value.pageNum = pageNum
  initBigTypeList()
}
const baseUrl = getServerUrl()

const dialogVisible = ref(false)
const id = ref(-1)
const dialogTitle = ref('')
const handleDialogValue = (bigTypeId) => {
  if (bigTypeId) {
    id.value = bigTypeId
    dialogTitle.value = '修改商品大类'
  } else {
    id.value = -1
    dialogTitle.value = '添加商品大类'
  }
  dialogVisible.value = true
}

const handleDelete = (id) => {
  ElMessageBox.confirm(
  '你确定要删除这条订单吗?',
  '系统提示',
  {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }
).then( async () => {
  const res = await axios.get(`admin/bigType/delete/${id}`)
  if (res.data.code == 0) {
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
    initBigTypeList()
  } else {
    ElMessage({
      type: 'error',
      message: res.data.msg
    })
  }
  }).catch(() => {

  })
}

const imageDialogVisible = ref(false)
const imageDialogValue = ref({})
const handleImageDialogValue = (row) => {
  imageDialogVisible.value = true
  imageDialogValue.value = JSON.parse(JSON.stringify(row))
}
</script>

<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="7">
        <el-input placeholder="请输入商品大类名称..." v-model="queryForm.query" clearable></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initBigTypeList">搜索</el-button>
      <el-button type="primary" :icon="DocumentAdd" @click="handleDialogValue()">添加商品大类</el-button>
    </el-row>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="id" label="ID" width="150" />
      <el-table-column prop="name" label="商品大类名称" width="300" />
      <el-table-column prop="image" label="商品大类图片">
        <template v-slot="scope">
          <img :src="baseUrl+'image/bigType/'+scope.row.image" width="80" height="80">
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="商品大类描述" />

      <el-table-column prop="action" label="操作" width="300" fixed="right">
        <template v-slot="scope">
          <el-button type="success" @click="handleImageDialogValue(scope.row)">更换图片</el-button>
          <el-button type="primary" :icon="Edit" @click="handleDialogValue(scope.row.id)"></el-button>
          <el-button type="danger" :icon="Delete" @click="handleDelete(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="queryForm.pageNum"
      v-model:page-size="queryForm.pageSize"
      :page-sizes="[10, 20, 30, 40, 50]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>
  <Dialog v-model="dialogVisible" :dialogVisible="dialogVisible" :id="id" :dialogTitle="dialogTitle" @initBigTypeList="initBigTypeList"></Dialog>
  <ImageDialog v-model="imageDialogVisible" :imageDialogValue="imageDialogValue" @initBigTypeList="initBigTypeList"></ImageDialog>
</template>

<style scoped lang="scss">
.header{
  padding-bottom: 16px;
  box-sizing: border-box;
}

.el-pagination{
  padding-top: 15px;
  box-sizing: border-box;
}
</style>
